<?php
# autoHTML: the dynamic PHP5 library for fast website prototyping
# http://code.google.com/p/autohtml/
#
# Copyright (c) 2010, Shu Zong Chen
# http://freelanceDreams.com/
# All rights reserved. 
# 
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions are met: 
# 
#  * Redistributions of source code must retain the above copyright notice, 
#    this list of conditions and the following disclaimer. 
#  * Redistributions in binary form must reproduce the above copyright 
#    notice, this list of conditions and the following disclaimer in the 
#    documentation and/or other materials provided with the distribution. 
#  * Neither the name of autoHTML nor the names of its contributors 
#    may be used to endorse or promote products derived from this software 
#    without specific prior written permission. 
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.

class AUTOHTML{
	var $CONFIG;    //Settings, Preferences
	var $HEADER;    //Stores CSS,JS,META data
	function AUTOHTML($config){
		$this->CONFIG = $config;
		$this->HEADER = new AUTOHTML_HEADER();
		$this->ASSETS = new AUTOHTML_ASSET_MANAGER();
		
		//defaults
		$this->HEADER->HTTP_EQUIV->attach(
			array(
				'http-equiv'=>'content-type',
				'content'=>'text/html',
				'charset'=>'utf-8'
			)
		);
		$this->setDoctype($this->CONFIG['doctype']);
		switch($this->CONFIG['asset_engine']){
			case 'sqlite':
				$this->INTERFACE['sqlite'] = new SQLiteDatabase(path_join($this->CONFIG['path_assets'], $this->CONFIG['sqlite']['resource']));
				break;
			case 'file':
			default:
				break;
		}
	}
	function __construct($config){
		$this->AUTOHTML($config);
	}

	################ Wrappers for AUTOHEADER ##################
	function setDoctype($doctype){
		$this->HEADER->attachDocType($doctype);
	}
	function setTitle($title){
		$this->HEADER->setTitle($title);
	}
	function attachCSS($file, $style = 'IMPORT', $media = ''){
		/*
			Use to automatically render CSS include tag in modes:
				IMPORT, LINK, INLINE
			Use a leading slash if you are using a direct link,
			no leading slash to automatically attach CSS folder.
		*/
		if (substr($file, 0, 1) == "/"){
			$fn = $file;
		}else{
			$fn = "{$this->CONFIG["http_css"]}/{$file}";
		}
		$this->HEADER->CSS->attach(array('uri'=>$fn, 'style'=>$style, 'media'=>$media));
	}
	function attachJS($file, $style = 'INLINE'){
		/*
			Use to automatically render CSS include tag in modes:
				IMPORT, LINK, INLINE
			Use a leading slash if you are using a direct link,
			no leading slash to automatically attach CSS folder.
		*/
		if (substr($file, 0, 1) == "/"){
			$fn = $file;
		}else{
			$fn = "{$this->CONFIG["http_js"]}/{$file}";
		}
		$this->HEADER->JS->attach(array('uri'=>$fn, 'style'=>$style));
	}
	function attachASSET($asset){
		return $this->ASSETS->attachASSET($asset);
	}
	############## Buffering Functions ########################
	function fetch($target){
		return $this->ASSETS->fetch($target);
	}
	function attach($target, $element, $override=false){
		$this->ASSETS->attach($target, $element, $override);
	}
	function attachFile($target, $filename, $override=false){
		//Append $filename to array $target
		$this->attachString($target, "%file:$filename%", $override);
	}
	function startBuffering(){
		ob_start();
	}
	function attachBuffer($target, $override=False){
		$this->attach($target, ob_get_clean(), $override);
	}
	function publish($no_headers=false){
		$this->attachBuffer('body');
		if ($no_headers===false){
			$this->HEADER->render();
		}
		echo '<body>', "\n";
		$this->ASSETS->render('body');
		echo '</body>', "\n";
		echo '</html>';
		return true;
	}
}